package best.driver.android.Activity.Order.Adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import best.driver.android.Activity.Order.OrderOperationActivity;
import best.driver.android.Beans.OrderAddresses;
import best.driver.android.Beans.OrderListInfo;
import best.driver.android.Beans.OrderListItemInfo;
import best.driver.android.Beans.OrderStatus;
import best.driver.android.R;
import best.driver.android.Utils.TimeUtil;
import best.driver.android.manager.OrderManager;
import butterknife.ButterKnife;
import butterknife.InjectView;

import java.util.ArrayList;
import java.util.List;

public class CurrentOrdersItemAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    private Context mContext;

    private ArrayList<OrderListInfo.Records> mOrderItems = new ArrayList<OrderListInfo.Records>();

    public CurrentOrdersItemAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(List<OrderListInfo.Records> list) {
        mOrderItems.clear();
        mOrderItems.addAll(list);
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mOrderItems.size();
    }

    @Override
    public OrderListInfo.Records getItem(int position) {
        return mOrderItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = mInflater.inflate(R.layout.current_orders_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, OrderOperationActivity.class);
                if(!TextUtils.isEmpty(mOrderItems.get(position).orderId)) {
                    intent.putExtra("orderId", mOrderItems.get(position).orderId);
                    mContext.startActivity(intent);
                }
            }
        });
        setConvertView(holder, position);
        return convertView;
    }

    private void setConvertView(ViewHolder holder, int position) {
        OrderListInfo.Records item = getItem(position);
        if(item == null || item.orderAddresses == null) {
            return;
        }
        for(int i = 0; i < item.orderAddresses.size(); i++) {
            OrderAddresses addresses = item.orderAddresses.get(i);
            if(addresses.flag.equals(OrderStatus.SHIPPER_ADDRESS)) {
                holder.mTvOrderStartAddress.setText(addresses.address);
            } else if(addresses.flag.equals(OrderStatus.RECEIVER_ADDRESS)) {
                holder.mTvOrderDestination.setText(addresses.address);
            }
        }
        if(OrderManager.getInstance().isProcessing(item.status) || OrderStatus.RETURNING.equals(item.status)) {
            holder.mTvTimeDesc.setText(OrderStatus.getInstance().getOrderStatusMap().get(item.status));
            holder.mTvTimeDesc.setTextColor(mContext.getResources().getColor(R.color.best_red));
        } else {
            holder.mTvTimeDesc.setText(TimeUtil.formatTime(item.orderTime));
            holder.mTvTimeDesc.setTextColor(mContext.getResources().getColor(R.color.a4));
        }

        if(OrderStatus.WAIT_START.equals(item.status)) {
            holder.mTvStartTime.setVisibility(View.VISIBLE);
            holder.mTvStartTime.setText(TimeUtil.formatTime(item.bookTime));
        } else {
            holder.mTvStartTime.setVisibility(View.GONE);
        }

        if(OrderStatus.ONE_TO_ONE.equals(item.transportMode)) {
            holder.mTvLoadMulti.setVisibility(View.GONE);
            holder.mTvUnloadMulti.setVisibility(View.GONE);
        } else if (OrderStatus.ONE_TO_N.equals(item.transportMode)) {
            holder.mTvLoadMulti.setVisibility(View.GONE);
            holder.mTvUnloadMulti.setVisibility(View.VISIBLE);
        } else if (OrderStatus.N_TO_ONE.equals(item.transportMode)) {
            holder.mTvLoadMulti.setVisibility(View.VISIBLE);
            holder.mTvUnloadMulti.setVisibility(View.GONE);
        } else {
            holder.mTvLoadMulti.setVisibility(View.GONE);
            holder.mTvUnloadMulti.setVisibility(View.GONE);
        }
    }

    static class ViewHolder {


        @InjectView(R.id.tv_order_start_address)
        TextView mTvOrderStartAddress;

        @InjectView(R.id.tv_order_destination)
        TextView mTvOrderDestination;

        @InjectView(R.id.tv_load_multi)
        TextView mTvLoadMulti;

        @InjectView(R.id.tv_unload_multi)
        TextView mTvUnloadMulti;

        @InjectView(R.id.tv_time_desc)
        TextView mTvTimeDesc;

        @InjectView(R.id.tv_start_time)
        TextView mTvStartTime;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
